﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.IRepository
{
    public interface IBaseRepository
    {
        #region 异步

        /// <summary>
        /// 根据条件查询第一条数据
        /// 例：Select * from Table Where Id =@Id  Param： new{Id=1}
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        public Task<T> QueryFirstOrDefaultAsync<T>(string Sql, object Param) where T : class;
        public Task<T> QueryFirstOrDefaultAsync<T>(string Sql) where T : class;

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="Sql"></param>
        /// <returns></returns>
        public Task<IEnumerable<T>> QueryAllAsync<T>(string Sql) where T : class;
        /// <summary>
        /// 根据条件查询所有
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        public Task<IEnumerable<T>> QueryAsync<T>(string Sql, object Param) where T : class;


        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="model">模型</param>
        /// <param name="FieldName">主键字段</param>
        /// <param name="IsIdentity">是否自增</param>
        /// <returns></returns>
        public Task<bool> InsertAsync<T>(T model, string FieldName = "Id", bool IsIdentity = true) where T : class;


        /// <summary>
        /// 插入单条数据返回自增列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model">模型</param>
        /// <param name="FieldName">主键字段</param>
        /// <param name="IsIdentity">是否自增</param>
        /// <returns></returns>
        public  Task<int> InsertReturnIdAsync<T>(T model, string FieldName = "Id") where T : class;
        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public Task<bool> InsertAsync<T>(IEnumerable<T> list, string FieldName = "Id", bool IsIdentity = true) where T : class;


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Task<bool> DeleteAsync<T>(T model) where T : class;


        /// <summary>
        /// 根据主键 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public Task<bool> DeleteAsync(string Sql, object Param);

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public Task<bool> DeleteAsync<T>(IEnumerable<T> list) where T : class;


        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        public Task<int> GetRecoreCountAsync(string Sql, object Param);
        public Task<object> GetRecoreAsync(string Sql, object Param);
        public Task<object> GetRecoreAsync(string Sql);
        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        public Task<int> GetRecoreCountAsync(string Sql);



        public Task<bool> UpdateAsync(string sql, object Param);
        /// <summary>
        /// 事务修改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Param"></param>
        /// <returns></returns>
        public Task<bool> UpdateTranAsync(string sql, object Param);
        public Task<bool> UpdateAsync<T>(T model) where T : class;

        /// <summary>
        /// 事务级联更新 注意：会先清除所有该单据子项数据在进行新增
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="model"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public Task<bool> UpdateTranAsync<T, T2>(T model, IEnumerable<T2> list, string InnerFieldName = "Default") where T : class;


        /// <summary>
        /// 批量修改，其中从表Id是自增列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="model"></param>
        /// <param name="list"></param>
        /// <param name="FieldName"></param>
        /// <param name="InnerFieldName">副表关联主表的字段名默认：主表名+Id</param>
        /// <param name="MasterInnerFieldName">主表被关联的字段名，默认为Id</param>
        /// <returns></returns>
        public Task<bool> UpdateIdentityTranAsync<T, T2>(T model, IEnumerable<T2> list, string FieldName = "Id", string InnerFieldName = "Default", string MasterInnerFieldName = "Id") where T : class;
        public Task<bool> UpdateIdentityTranAsync<T, T2, T3>(T model, IEnumerable<T2> list, IEnumerable<T3> list2, string FieldName = "Id", string InnerFieldName = "Default", string MasterInnerFieldName = "Id") where T : class;

        public Task<bool> UpdateTranAsync<T, T2, T3>(T model, IEnumerable<T2> list, IEnumerable<T3> T3List) where T : class;
        public Task<bool> InsertIdentityTranAsync<T, T2, T3>(T model, IEnumerable<T2> list, IEnumerable<T3> list2, string FieldName = "Id", string InnerFieldName = "Default") where T : class;


        /// <summary>
        /// 使用事务执行  新增两张表数据，其中 表二是批量的
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="model"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public Task<bool> InsertTranAsync<T, T2>(T model, IEnumerable<T2> list) where T : class;
        public Task<bool> InsertTranAsync<T, T2, T3>(T model, IEnumerable<T2> list, IEnumerable<T3> T3List, string FieldName = "Id", bool IsIdentity = true) where T : class;


        /// <summary>
        /// 使用事务插入数据，主表模型 从表集合，其中主表从表都是Id自增
        /// </summary>
        /// <typeparam name="T">主表</typeparam>
        /// <typeparam name="T2">从表</typeparam>
        /// <param name="model">主表模型</param>
        /// <param name="list">从表集合</param>
        /// <param name="FieldName">主键名称：默认Id</param>
        /// <param name="InnerFieldName">关联主表的字段名称</param>
        /// <returns></returns>
        public Task<bool> InsertIdentityTranAsync<T, T2>(T model, IEnumerable<T2> list, string FieldName = "Id", string InnerFieldName = "Default") where T : class;

        #endregion
    }
}
